home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter09 / BouncingPanel.java < prev    next >
Text File  |  2000-08-27  |  2KB  |  73 lines

  1.  
  2. package applets;
  3.  
  4. import shout3d.*;
  5. import shout3d.core.*;
  6. import shout3d.math.*;
  7.  
  8.  
  9. public class BouncingPanel extends Shout3DPanel implements RenderObserver{
  10.    
  11.    
  12.    Transform t;
  13.    float cycleAngle = 0.0f;
  14.    float startHeight = 3.0f;
  15.    float startCycleTime = 2.0f;
  16.    float declineFactor = 1.0f;
  17.    boolean bouncing = true;
  18.  
  19.    public BouncingPanel (Shout3DApplet applet){
  20.       super(applet);
  21.    }
  22.    
  23.    
  24.      public void customInitialize() {
  25.  
  26.       getRenderer().addRenderObserver(this, null);
  27.       t = (Transform) getNodeByName("trans");
  28.    
  29.    }
  30.  
  31.  
  32.    protected void finalize()  { 
  33.       getRenderer().removeRenderObserver(this);
  34.    }
  35.  
  36.  
  37.    public void onPreRender (Renderer r, Object o) {
  38.    
  39.      if (bouncing) {
  40.  
  41.       //compute decline factor at 8 percent per second
  42.       declineFactor = declineFactor - (.08f/getFramesPerSecond());
  43.  
  44.       float cycleTime = startCycleTime * declineFactor;
  45.       
  46.       //use 3.14 because there are
  47.       //two complete bouces in a cycle
  48.       float deltaAngle = (3.14f/cycleTime)/getFramesPerSecond();
  49.       cycleAngle = cycleAngle + deltaAngle;
  50.  
  51.       float height = startHeight * declineFactor;
  52.       float yPos   = (float) (Math.cos(cycleAngle) * height);
  53.  
  54.       //no negative height
  55.       yPos = Math.abs(yPos);
  56.  
  57.       t.translation.set1Value(1, yPos);
  58.       
  59.       //stop bouncing when height is low
  60.       if (height < .1) {
  61.          bouncing = false;
  62.       }
  63.      }
  64.       
  65.    }
  66.    
  67.  
  68.    public void onPostRender (Renderer r, Object o) {
  69.    
  70.    
  71.    }
  72.  
  73. } //end of class